/*
 * contrib/btree_gist/btree_enum.c
 */
#include "postgres.h"

#include "btree_gist.h"
#include "btree_utils_num.h"
#include "fmgr.h"
#include "utils/builtins.h"

/* 枚举实际上是 Oids，因此我们只使用相同的结构 */

typedef struct
{
	Oid			lower;
	Oid			upper;
} oidKEY;

/*
** 枚举操作
*/
PG_FUNCTION_INFO_V1(gbt_enum_compress);
PG_FUNCTION_INFO_V1(gbt_enum_fetch);
PG_FUNCTION_INFO_V1(gbt_enum_union);
PG_FUNCTION_INFO_V1(gbt_enum_picksplit);
PG_FUNCTION_INFO_V1(gbt_enum_consistent);
PG_FUNCTION_INFO_V1(gbt_enum_penalty);
PG_FUNCTION_INFO_V1(gbt_enum_same);


static bool fc_gbt_enumgt(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	return DatumGetBool(CallerFInfoFunctionCall2(enum_gt, fc_flinfo, InvalidOid,
												 ObjectIdGetDatum(*((const Oid *) fc_a)),
												 ObjectIdGetDatum(*((const Oid *) fc_b))));
}
static bool fc_gbt_enumge(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	return DatumGetBool(CallerFInfoFunctionCall2(enum_ge, fc_flinfo, InvalidOid,
												 ObjectIdGetDatum(*((const Oid *) fc_a)),
												 ObjectIdGetDatum(*((const Oid *) fc_b))));
}
static bool fc_gbt_enumeq(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	return (*((const Oid *) fc_a) == *((const Oid *) fc_b));
}
static bool fc_gbt_enumle(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	return DatumGetBool(CallerFInfoFunctionCall2(enum_le, fc_flinfo, InvalidOid,
												 ObjectIdGetDatum(*((const Oid *) fc_a)),
												 ObjectIdGetDatum(*((const Oid *) fc_b))));
}
static bool fc_gbt_enumlt(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	return DatumGetBool(CallerFInfoFunctionCall2(enum_lt, fc_flinfo, InvalidOid,
												 ObjectIdGetDatum(*((const Oid *) fc_a)),
												 ObjectIdGetDatum(*((const Oid *) fc_b))));
}

static int fc_gbt_enumkey_cmp(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	oidKEY	   *fc_ia = (oidKEY *) (((const Nsrt *) fc_a)->t);
	oidKEY	   *fc_ib = (oidKEY *) (((const Nsrt *) fc_b)->t);

	if (fc_ia->lower == fc_ib->lower)
	{
		if (fc_ia->upper == fc_ib->upper)
			return 0;

		return DatumGetInt32(CallerFInfoFunctionCall2(enum_cmp, fc_flinfo, InvalidOid,
													  ObjectIdGetDatum(fc_ia->upper),
													  ObjectIdGetDatum(fc_ib->upper)));
	}

	return DatumGetInt32(CallerFInfoFunctionCall2(enum_cmp, fc_flinfo, InvalidOid,
												  ObjectIdGetDatum(fc_ia->lower),
												  ObjectIdGetDatum(fc_ib->lower)));
}

static const gbtree_ninfo tinfo =
{
	gbt_t_enum,
	sizeof(Oid),
	8,							/* sizeof(gbtreekey8) */
	fc_gbt_enumgt,
	fc_gbt_enumge,
	fc_gbt_enumeq,
	fc_gbt_enumle,
	fc_gbt_enumlt,
	fc_gbt_enumkey_cmp,
	NULL						/* 目前至少不支持 KNN */
};


/**************************************************
 * 枚举操作
 **************************************************/


Datum gbt_enum_compress(PG_FUNCTION_ARGS)
{
	GISTENTRY  *fc_entry = (GISTENTRY *) PG_GETARG_POINTER(0);

	PG_RETURN_POINTER(gbt_num_compress(fc_entry, &tinfo));
}

Datum gbt_enum_fetch(PG_FUNCTION_ARGS)
{
	GISTENTRY  *fc_entry = (GISTENTRY *) PG_GETARG_POINTER(0);

	PG_RETURN_POINTER(gbt_num_fetch(fc_entry, &tinfo));
}

Datum gbt_enum_consistent(PG_FUNCTION_ARGS)
{
	GISTENTRY  *fc_entry = (GISTENTRY *) PG_GETARG_POINTER(0);
	Oid			fc_query = PG_GETARG_OID(1);
	StrategyNumber fc_strategy = (StrategyNumber) PG_GETARG_UINT16(2);

	/* Oid		subtype = PG_GETARG_OID(3); */
	bool	   *fc_recheck = (bool *) PG_GETARG_POINTER(4);
	oidKEY	   *fc_kkk = (oidKEY *) DatumGetPointer(fc_entry->key);
	GBT_NUMKEY_R fc_key;

	/* 由此函数服务的所有案例都是精确的 */
	*fc_recheck = false;

	fc_key.lower = (GBT_NUMKEY *) &fc_kkk->lower;
	fc_key.upper = (GBT_NUMKEY *) &fc_kkk->upper;

	PG_RETURN_BOOL(gbt_num_consistent(&fc_key, (void *) &fc_query, &fc_strategy,
									  GIST_LEAF(fc_entry), &tinfo,
									  fcinfo->flinfo));
}

Datum gbt_enum_union(PG_FUNCTION_ARGS)
{
	GistEntryVector *fc_entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
	void	   *fc_out = palloc(sizeof(oidKEY));

	*(int *) PG_GETARG_POINTER(1) = sizeof(oidKEY);
	PG_RETURN_POINTER(gbt_num_union((void *) fc_out, fc_entryvec, &tinfo, fcinfo->flinfo));
}


Datum gbt_enum_penalty(PG_FUNCTION_ARGS)
{
	oidKEY	   *fc_origentry = (oidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
	oidKEY	   *fc_newentry = (oidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
	float	   *fc_result = (float *) PG_GETARG_POINTER(2);

	penalty_num(fc_result, fc_origentry->lower, fc_origentry->upper, fc_newentry->lower, fc_newentry->upper);

	PG_RETURN_POINTER(fc_result);
}

Datum gbt_enum_picksplit(PG_FUNCTION_ARGS)
{
	PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
										(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
										&tinfo, fcinfo->flinfo));
}

Datum gbt_enum_same(PG_FUNCTION_ARGS)
{
	oidKEY	   *fc_b1 = (oidKEY *) PG_GETARG_POINTER(0);
	oidKEY	   *fc_b2 = (oidKEY *) PG_GETARG_POINTER(1);
	bool	   *fc_result = (bool *) PG_GETARG_POINTER(2);

	*fc_result = gbt_num_same((void *) fc_b1, (void *) fc_b2, &tinfo, fcinfo->flinfo);
	PG_RETURN_POINTER(fc_result);
}
